package com.transpeed.payservice.Utils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;



/**
 * TODO 银联相关支付工具类
 * @author lizhi
 * @date 2017年4月10日
 * 更新日志
 * 2017年4月10日 lizhi 首次创建
 *
 */

public class UmsPayUtils {

	/**
	 * 
	 * 生成随机的qrCodeId 生成规范 qrCodeId ={系统编号(4位)}{时间(yyyyMMddmmHHssSSS)(17位)}{7位随机数}
	 * @param qrCodeIdSystemNumber 系统编号（银联分配） 
	 * @return
	 */
	
	public static String getQRCodeId(String qrCodeIdSystemNumber) {
		SimpleDateFormat qrCodeIdTimeFormat = new SimpleDateFormat("yyyyMMddmmHHssSSS");// 设置日期格式
		String qrCodeId = qrCodeIdSystemNumber
				+ qrCodeIdTimeFormat.format(new Date())
				+ NumberUtil.getFixLenthString(7);
		return qrCodeId;
	}

	/**
	 * @return 当天日期
	 */
	public static String getDate() {

		SimpleDateFormat requestTimeFormat = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		
		String RequestTimestamp = requestTimeFormat.format(new Date());
		return RequestTimestamp;
	}
	
	
	/**
	 * @return 当前时间戳
	 */
	
	public static String getRequestTimestamp() {
		
		SimpleDateFormat requestTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		String RequestTimestamp = requestTimeFormat.format(new Date());
		return RequestTimestamp;
	}
	
	
	
	/**
	 * @return 当前时间N小时后的时间戳
	 */
	public static String getExpireTime(int hour) {
		
		SimpleDateFormat requestTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(requestTimeFormat.parse(getRequestTimestamp()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.HOUR_OF_DAY, hour);
		String ExpireTime = requestTimeFormat.format(cal.getTime());// 过期时间，默认为获取的2小时后
			
		return ExpireTime;
	}
	
	
     /**
     * 自主生成二维码 生成规则为 {baseUrl}/bills/qrCode.do?id={qrCodeId}
     * @param qrCodeId
     * @return qrCodeUrl
     */
    public static String getQRCodeUrl(String qrCodeId) {
    	 
    	 String baseUrlTest = "http://umspay.izhong.me";//银联接口地址
    	 String qrCodeUrl = baseUrlTest + "/bills/qrCode.do?id=" + qrCodeId;
    	 
		return qrCodeUrl;
	}
    
    
    /**
     * @param SystemNumber
     * @return BillNo
     */
    public static String getBillNo(String SystemNumber) {
   	 
    	String BillNo = SystemNumber + System.currentTimeMillis() + (long) (Math.random() * 10000000L);
   	 
		return BillNo;
	}
    
    public static HashMap<String, Object>  constructHashMap_getQRCode(String BillDesc,String TotalAmount,String BillNo , String qrCodeId ,String SystemNumber) throws IOException {
      	
    	HashMap<String, Object> test = new HashMap<String, Object>();
		test.put("msgId", "800000000010");
		test.put("msgSrc", "WWW.TRANSPEEDSG.COM");
		test.put("msgType", "bills.getQRCode");
		test.put("requestTimestamp", UmsPayUtils.getRequestTimestamp());
		test.put("srcReserve", "1234");
		test.put("mid", "911000000000000");
		test.put("tid", "88880001");
		test.put("instMid", "QRPAYDEFAULT");
		test.put("billNo", BillNo);
		test.put("billDate", UmsPayUtils.getDate());	
		test.put("billDesc", BillDesc);
		test.put("totalAmount", TotalAmount);	
		test.put("memberId", "1234");
		test.put("counterNo", "8888");
		test.put("expireTime", UmsPayUtils.getExpireTime(2));
		test.put("notifyUrl","http://112.81.110.151:4000/transpeed-PayService/UmsPayAPI/ReceiveUmsPayTradeInfo");
		test.put("returnUrl", "www.baidu.com");
		test.put("qrCodeId", qrCodeId);
		test.put("systemId", SystemNumber);
		test.put("sign", MD5Utils.getSignature(test,"DSANEdAEJCDNTDaS7ye8jFfBSzNGTZkR5SFzHfn8TWkSd5fK"));
		return test;
	}
    
	public static HashMap<String, Object> constructHashMap_Query(String billNo,String billDate) throws IOException {

		HashMap<String, Object> test = new HashMap<String, Object>();
		test.put("msgId", "800000000010");
		test.put("msgSrc", "WWW.TRANSPEEDSG.COM");
		test.put("msgType", "bills.query");
		test.put("requestTimestamp", UmsPayUtils.getRequestTimestamp());
		test.put("srcReserve", "1234");
		test.put("mid", "911000000000000");
		test.put("tid", "88880001");
		test.put("instMid", "QRPAYDEFAULT");
		test.put("billNo", billNo);
		test.put("billDate", billDate);
		test.put("sign", MD5Utils.getSignature(test,"DSANEdAEJCDNTDaS7ye8jFfBSzNGTZkR5SFzHfn8TWkSd5fK"));
		return test;
	}
    
	
}
